﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.Utilities;
using FinanSoft.PresentacionInventario;
using FinanSoft.BusinessLayer;
using FinanSoft.Entities;

namespace FinanSoft.PresentacionComprasyVentas
{
    public partial class frmAgregarArticulo : Form
    {

        private int[] _storeRoomIds;
        private int[] _itemIds;
        private int[] _taxIds;
        private frmOrden _parent;

        public frmAgregarArticulo(frmOrden parent)
        {
            _parent = parent;
            InitializeComponent();
            fillComboBoxStores();
            fillComboBoxArticle();
            fillComboBoxTax();
        }

        // Llenar el combobox de todas las bodegas
        private void fillComboBoxStores()
        {
            List<StoreRoom> stores = StoreRoomManager.GetInstance().GetStoreRooms();
            _storeRoomIds = new int[stores.Count];
            int srIndex = 0;
            if (!StoreRoomManager.GetInstance().IsError)
                foreach (StoreRoom store in stores)
                {
                    cmbAccounts.Items.Add(store.Name);
                    _storeRoomIds[srIndex++] = store.Id;
                }
            else
            {
                MessageBox.Show(StoreRoomManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            if(srIndex > 0) cmbAccounts.SelectedIndex = 0;
        }

        //Método para llenar el combobox  de articulos
        private void fillComboBoxArticle()
        {
            List<Item> items = ItemManager.GetInstance().GetItems();
            _itemIds = new int[items.Count];
            int itemIndex = 0;
            foreach (Item item in items)
            {
                comboDesc.Items.Add(item.Descripción);
                _itemIds[itemIndex++] = item.Id; 
            }
            if (itemIndex > 0) comboDesc.SelectedIndex = 0;
        }

        //Llenar combobox de los impuestos
        private void fillComboBoxTax()
        {
            List<Tax> impuestos = ConfigurationManager.GetInstance().GetTaxes();
            _taxIds = new int[impuestos.Count];
            int taxIndex = 0;
            if (!StoreRoomManager.GetInstance().IsError)
            {
                foreach (Tax impuesto in impuestos)
                {
                    cmbTax.Items.Add(impuesto.Nombre);
                    _taxIds[taxIndex++] = impuesto.IdImpuesto;
                }
            }
            else
            {
                MessageBox.Show(StoreRoomManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            if(taxIndex > 0) cmbTax.SelectedIndex = 0;
        }

        private bool validateInformation()
        {
            return (cmbAccounts.SelectedIndex != Constants.INVALID_ID && comboDesc.SelectedIndex != Constants.INVALID_ID &&
                cmbTax.SelectedIndex != Constants.INVALID_ID && txtPrice.Text.Trim().Length != 0 &&
                txtCant.Text.Trim().Length != 0);
        }

        // Método para agragar Articulos
        private void saveArticle() 
        {
            if (validateInformation())
            {
                Double cantidad = int.Parse(txtCant.Text);
                Double precio = int.Parse(txtPrice.Text);
                String nombreTipoImpuesto = cmbTax.Text;
                _parent.AddItem(ItemManager.GetInstance().GetItem(_itemIds[comboDesc.SelectedIndex]),
                                StoreRoomManager.GetInstance().GetStoreRoom(_storeRoomIds[cmbAccounts.SelectedIndex]),
                                cantidad, precio, ConfigurationManager.GetInstance().GetTax(_taxIds[cmbTax.SelectedIndex]));
                Close();
            }
            else
                MessageBox.Show(MessageConstants.MISSING_FIELDS, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            saveArticle();
        }

        private void txtCant_KeyPress(object sender, KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }

        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }
    }
}
